package cn.jiyun.controller;

import cn.jiyun.pojo.Ect;
import cn.jiyun.pojo.Roles;
import cn.jiyun.pojo.Users;
import cn.jiyun.service.EctService;
import cn.jiyun.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @Autowired
    private EctService ectService;



    @RequestMapping("/addEct")
    public String addEct(Ect ect,Model model) {
        this.usersService.addEct(ect);

        List<Ect> list = this.usersService.listUser();
        model.addAttribute("list", list);
        return "list";
    }

    @RequestMapping("/deleteEct")
    public String deleteEct(Integer id,Model model) {
        this.usersService.deletes(id);

        List<Ect> list = this.usersService.listUser();
        model.addAttribute("list", list);
        return "list";
    }


    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page) {
        return page;
    }

    @RequestMapping("/")
    public String showRegister(){
        return "login";
    }

    @RequestMapping("/toadd")
    public String showAdd(){
        return "add";
    }

    @RequestMapping("/listEct")
    public String listEct(Model model) {
        List<Ect> list = this.usersService.listUser();

        model.addAttribute("list", list);
        return "list";
    }

    @RequestMapping("/listRole")
    public String listRole(Model model) {
        List<Roles> role = this.usersService.listRole();
        model.addAttribute("role", role);
        return "regist";
    }

    @RequestMapping("/login")
    public String login(Model model, Users users, HttpSession session) {
        Users user = usersService.login(users);
        if(user!=null){
            session.setAttribute("user", user);
            return "redirect:/users/listEct";
        }
        return "login";
    }


}
